from PyQt5.QtCore import Qt
from PyQt5.QtGui import QColor
import numpy as np


class Layer:
    def __init__(self):
        self.name = None  # 图层名称
        self.geometryType = None
        self.data = None  # 以DataFrame的形式，包含索引、几何数据、属性
        self.xMin = np.inf
        self.xMax = -np.inf
        self.yMin = np.inf
        self.yMax = -np.inf
        self.selectionSet = []  # 选择集，存储被选中的图形的索引
        self.lineColor = Qt.black
        self.ptSize = 5
        self.lineWidth = 3
        self.lineStyle = Qt.SolidLine
        self.fillColor = self.__random_color()
        self.visible = True
        self.selectionBorderColor = Qt.cyan
        self.editable = False

    @staticmethod
    def __random_color():
        return QColor(*np.random.randint(0, 256, size=3))

    def setData(self, df):
        self.data = df

    def getBBox(self):
        return self.xMin, self.xMax, self.yMin, self.yMax

    def setBBox(self, xMin, xMax, yMin, yMax):
        self.xMin = xMin
        self.xMax = xMax
        self.yMin = yMin
        self.yMax = yMax

    def getdata(self):
        return self.data

    def setGeometryType(self, geometryType):
        self.geometryType = geometryType
        if self.geometryType == "LineString":
            self.lineColor = self.__random_color()

    def setSelectionSet(self, selectionSet):
        self.selectionSet = selectionSet

    def getSelectionSet(self):
        return self.selectionSet

    def updateBBox(self):
        """
        遍历所有图形，重新求layer的bbox

        :return:
        """
        bbox_series = self.data["my_geom"].apply(lambda x: x.bbox)
        self.xMin = np.min(bbox_series.apply(lambda x: x[0]))
        self.xMax = np.max(bbox_series.apply(lambda x: x[1]))
        self.yMin = np.min(bbox_series.apply(lambda x: x[2]))
        self.yMax = np.max(bbox_series.apply(lambda x: x[3]))
